package com.zjl.hiring.company.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.zjl.hiring.common.Constant;
import com.zjl.hiring.common.DBObject;
import com.zjl.hiring.common.util.DBUtil;
import com.zjl.hiring.company.entity.ApplicationEntity;

/**
 * 
 * @author Administrator
 *
 */
public class ApplicationDao {

	public int add(int positionId, int talentId, int resumeId) {
		int row = 0;
		String sql = "INSERT INTO application VALUES(NULL ,? ,? ,? ,?, NOW(), NULL)";
		try {
			List<Object> paramList = new ArrayList<>();
			paramList.add(positionId);
			paramList.add(talentId);
			paramList.add(resumeId);
			paramList.add(Constant.APPLY_NO_CHECK);
			row = DBUtil.executeUpdate(sql, paramList);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return row;
	}

	/**
	 * 判断该职位是否已经申请
	 * 
	 * @param positionId
	 * @param talentId
	 * @return
	 */
	public ApplicationEntity applyed(int positionId, int talentId) {
		ApplicationEntity application = null;
		String sql = "SELECT * FROM application WHERE position_id=? AND talent_id=?";
		DBObject dbObject = null;
		try {
			dbObject = DBUtil.executeQuery(sql, positionId, talentId);
			ResultSet resultSet = dbObject.getResultSet();
			if (resultSet.next()) {
				application = new ApplicationEntity();
				application.setStatus(resultSet.getInt("status"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return application;
	}

	public List<ApplicationEntity> listAllapplication(int talentId) {
		List<ApplicationEntity> applicationList = new ArrayList<>();
		ApplicationEntity application = null;
		String sql = "SELECT a.id, p.name AS 'position_name', c.name AS 'company_name', a.status, a.app_time FROM application a, company c, position p WHERE a.position_id=p.id AND p.company_id=c.id AND talent_id=?";
		DBObject dbObject = null;
		try {
			dbObject = DBUtil.executeQuery(sql, talentId);
			ResultSet resultSet = dbObject.getResultSet();
			while (resultSet.next()) {
				application = new ApplicationEntity();
				application.setId(resultSet.getInt("id"));
				application.setPositionName(resultSet
						.getString("position_name"));
				application.setCompanyName(resultSet.getString("company_name"));
				application.setStatus(resultSet.getInt("status"));
				application.setAppTime(resultSet.getTimestamp("app_time"));
				applicationList.add(application);
			}
		} catch (SQLException e) {
			applicationList = Collections.emptyList();
			e.printStackTrace();
		} finally {
			DBUtil.close(dbObject);
		}
		return applicationList;
	}

	public int deleteApplication(int applicationId) {
		int row = 0;
		String sql = "DELETE FROM application WHERE id=?";
		try {
			row = DBUtil.executeUpdate(sql, applicationId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return row;
	}
}
